package users

import (
	"errors"

	"gitee.com/xgotool/backend-framework-template/config"
	"gitee.com/xgotool/backend-framework-template/model"
)

// 获取用户详情信息
//
//	uid	用户ID
//	pem	是否强制获取（忽略status状态）
func Info(uid uint, pem ...bool) (*model.User, error) {
	conn, err := config.GetConnect()
	if err != nil {
		return nil, err
	}
	mod := model.User{}
	err = conn.Table(mod.TableName()).
		Select("id", "avatar", "user_name", "phone", "nickname", "email", "created_at", "last_login", "status").
		Where("id", uid).
		When(len(pem) == 0, "status", 1).
		First(&mod).
		Error
	if err != nil {
		return nil, err
	}
	if mod.Id == 0 {
		return nil, errors.New("用户不存在/已禁用")
	}
	mod.Avatar = config.GetUploadDomain(mod.Avatar)
	return &mod, nil
}
